#include "StdAfx.h"
#include "ObjectVisual.h"
#include "LayoutObject.h"
#include <qmath.h>
ObjectVisual::ObjectVisual(LayoutObject* obj) : QGraphicsItem(0), layoutObj_(obj)//,unusedRotation_(0)
{	
	setFlag(QGraphicsItem::ItemIsMovable, true);
	setFlag(QGraphicsItem::ItemIsSelectable, true);
	setFlag(ItemSendsGeometryChanges, true);

	// so that the object rotates around the origin
	setTransformOriginPoint(layoutObj_->Prefab()->Pixmap()->width()/2, layoutObj_->Prefab()->Pixmap()->height()/2);

	// For drawing the rect around our objects.
	RectPen_ = QPen(QBrush("#000000"), 6, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
	// Is there a better place to put this
}

ObjectVisual::~ObjectVisual()
{

}

QVariant ObjectVisual::itemChange(GraphicsItemChange change, const QVariant &value)
{
	int snappingValue = 50;

    if(change == ItemPositionChange)
	{
		
		/*float dx = 0, dy = 0;
		float rot = rotation();
		
		QPixmap* pixmap = layoutObj_->Prefab()->Pixmap();
		if(rotation() == 0) 
		{
			dx = 0; dy = 0;
		}
		else if(((int)rotation()) % 90 == 0) 
		{
			
			auto width = pixmap->width();
			auto height = pixmap->height();

			modf(pixmap->height() /  50.0, &dx);
			modf(pixmap->width() /  50.0, &dy);


			dx = ceil(dx) + 1; 
			dy = ceil(dy) + 1;

			
			dx *= 50;
			dy *= 50;

			dx -= pixmap->height();
			dy -= pixmap->width();

			dx /= -2;
			dy /= 2;
		}
		//previous behavior
		//dx = 0;
		//dy = 0;*/
		
		QPointF newPosition = value.toPointF();
		newPosition.setX(floor((newPosition.x()/snappingValue)) * snappingValue+layoutObj_->Prefab()->Pixmap()->width()/2);
		newPosition.setY(floor((newPosition.y()/snappingValue)) * snappingValue+layoutObj_->Prefab()->Pixmap()->height()/2);
		
		//newPosition += QPointF(dx, dy);
		

		layoutObj_->SetPosition(newPosition);
		return newPosition;

	}
	else if(change == ItemSelectedHasChanged)
	{
			//
		emit SelectionChanged(this);
		return value;
	}
	
    return QGraphicsItem::itemChange(change, value);
}


// Defines the outer bounds of what's being drawn. All painting must be restricted to inside an item's bounding rect hence the +6.
// nvm about the +6, border goes on the image now
QRectF ObjectVisual::boundingRect() const
{

	QPixmap* pixmap = layoutObj_->Prefab()->Pixmap();
	return QRectF(0, 0, pixmap->width() + 0, pixmap->height() + 0);
}

// paint is called every frame to paint objects.
void ObjectVisual::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	QPixmap* pixmap = layoutObj_->Prefab()->Pixmap();
	// offset the pixmap by 5 so we may draw the border when selected
	// nvm about the 5 up there, border now goes above image
	painter->drawPixmap(0, 0, pixmap->width(),
		pixmap->height(), *pixmap);

	// If our object is selected we want to draw a rect around it.
	if(isSelected())
	{
		//painter->setBrush(QBrush("#539e47"));
		//painter->setPen(QPen(QBrush("#539e47"), 10, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
		painter->setPen(RectPen_); // This honestly only needs to be called once but w/e
		painter->drawRect(3, 3, pixmap->width()-6, pixmap->height()-6);


	}
	else
	{		
		//painter->setBrush(QBrush("#000000"));
		//painter->setPen(QPen(QBrush("#000000"), 10));
		//painter->drawRect(0, 0, layoutObj_->GetPixmap()->width(),layoutObj_->GetPixmap()->height());
	}

	
}

/*void ObjectVisual::QMouseMoveEvent(QMouseEvent * Event)
{
	if(Event->modifier = Qt::ShiftModifier)
	{
		if(Event->MouseButtonDblClick)
		{
			qreal q = 90;
			setRotation(q);
		}
	}
	if(Event->HoverEnter)
	{
		qreal q = 90;
		setRotation(q);
	}

}*/

#define PI 3.1415926

void ObjectVisual::handleMouseDrag(QPointF start, QPointF end)
{
	
	auto startAngle = qAtan(((double)start.x())/start.y());
	auto endAngle = qAtan(((double)end.x())/end.y());
	//unusedRotation_ += startAngle - endAngle;
	

	//double completeRotations;
	//unusedRotation_ = modf(unusedRotation_ / (PI / 2), &completeRotations);
	
	//rotate(completeRotations);
	//setRotation(rotation() + (startAngle - endAngle) * (180.0 / PI));
	//setRotation(rotation() + (startAngle - endAngle) * (180.0 / PI));
	//rotate((startAngle - endAngle) * (180.0 / PI));

	layoutObj_->setOrientation(layoutObj_->Orientation()+(startAngle - endAngle) * (180.0 / PI)); 

	
	/*
	if(layoutObj_->Orientation() < 0)
	{
		layoutObj_->setOrientation((2*PI - layoutObj_->Orientation())* (180.0 / PI));
	}
	if(layoutObj_->Orientation() > 2*PI)
	{
		layoutObj_->setOrientation((layoutObj_->Orientation()-2*PI)* (180.0 / PI));
	}*/

		setRotation(floor(layoutObj_->Orientation() / 90) * 90 );
}